package com.example.demo.runner.code;

import com.example.demo.entity.User;
import com.example.demo.tool.FileTool;
import com.example.demo.tool.Global;

import java.io.IOException;

public class CRunner extends AbstractCodeRunner {
    private Process process = null;

    CRunner(User user) {
        super(user);
    }

    @Override
    public void compile() throws IOException {
        // TODO exe name?
        if (process != null) {
            return;
        }
        process = Runtime.getRuntime().exec("gcc -o textC " + getFilePath());
        try {
            process.waitFor();
        } catch (InterruptedException e) {
            e.printStackTrace();
            // TODO
            return;
        }
        process = Runtime.getRuntime().exec("./textC");
        subProcStdInStream = process.getOutputStream();
        subProcStdOutStream = process.getInputStream();
        subProcStdErrStream = process.getErrorStream();
    }

    private String getFilePath() {
        String path = Global.CODE_PATH + "plane/";
        FileTool.mkDir(path);
        String languageSuffix = ".c";
//        switch (code.languageSuffix){
//            case "C":
//                languageSuffix = ".c";break;
//            case "Python3":languageSuffix = ".py";break;
//            case "Java":languageSuffix = ".java";break;
//        }
        String filePath = path + getUser().getId() + languageSuffix;
        return filePath;
    }

    @Override
    public void stop() {
        super.stop();
        process.destroy();
    }
}
